package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.annotation.RequirePermission;
import com.woniuxy.entity.Bed;
import com.woniuxy.entity.Home;
import com.woniuxy.entity.User;
import com.woniuxy.service.bed.BedService;
import com.woniuxy.service.user.UserService;
import com.woniuxy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class BedController {
    @Autowired
    private BedService bedService;
    @Autowired
    private UserService userService;

    @RequirePermission("bed:find")
    @PostMapping("bed/findOne")
    public ResultVo findOne0(@RequestBody String str) {
        Bed bed = bedService.findOne(str);
        return bed != null ? ResultVo.success("ok", bed) : ResultVo.failure("no");
    }
//    @PostMapping("bed/findLike")
//    public ResultVo findLike(@RequestBody String str) {
//        str = "%" + str + "%";
//        List<Bed> bed = bedService.Like(str);
//        return bed != null ? ResultVo.success("ok", bed) : ResultVo.failure("no");
//    }

    @RequirePermission("bed:find")
    @PostMapping("bed/findAll")
    public ResultVo findAll0() {
        List<Bed> bed = bedService.findAll();
        return bed.size() > 0 ? ResultVo.success("ok", bed) : ResultVo.failure("no");
    }

    @RequirePermission("bed:find")
    @PostMapping("bed/findByPage")
    public ResultVo findByPage(@RequestBody Map<String, String> map) {
        String strPage = map.remove("page");
        String strSize = map.remove("size");


        PageInfo<Bed> bed = bedService.findByPage(Integer.valueOf(strPage), Integer.valueOf(strSize));
        return ResultVo.success(bed);
    }


    @MyLog("删除床位")
    @RequirePermission("bed:delete")
    @PostMapping("bed/deleteOne")
    public ResultVo deleteOne0(@RequestBody String str) {
        int bed = bedService.deleteOne(str);
        return bed > 0 ? ResultVo.success("ok", bed) : ResultVo.failure("no");
    }


    @MyLog("修改床位信息")
    @RequirePermission("bed:update")
    @PostMapping("bed/update")
    public ResultVo updateOne0(@RequestBody Bed bed) {
        int rows = bedService.updateOne(bed);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }
    @MyLog("添加床位")
    @RequirePermission("bed:save")
    @PostMapping("bed/save")
    public ResultVo save(@RequestBody Bed bed){
        int rows = bedService.save(bed);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }
    @PostMapping("bed/findAllHome")
    public ResultVo findAllHome(@RequestBody Map<String,String> map){
        String hId = map.remove("hId");
        String deptId = map.remove("deptId");
        List<Home> allHome = bedService.findAllHome(Integer.valueOf(hId),Integer.valueOf(deptId));
        return ResultVo.success(allHome);
    }

    @PostMapping("bed/findAllShareholder")
    public ResultVo findAllShareholder(){
        List<User> list = userService.findAll();
        return ResultVo.success(list);
    }
}
